import axios from "axios";
import store from "../store";

axios.defaults.withCredentials = true;
axios.defaults.baseURL = `http://localhost:3000/v1`;

axios.interceptors.request.use(
  (config) => {
    config.headers.authorization = store.state.token;
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

axios.interceptors.response.use(
  (response) => {
    let token = response.headers.authorization;
    if (token) {
      store.commit("setToken", token);
    }
    return response;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default axios;
